<!-- warehouse -->
<template>
  <div>
    <HeaderTab>
      <template #left>
        <el-form :inline="true" label-position="top" :model="formDate">
          <el-form-item label="仓库编号" prop="like_code">
            <el-input v-model="formDate.like_code" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="仓库名称" prop="like_name">
            <el-input v-model="formDate.like_name" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="仓库状态" prop="lstatus">
            <el-select v-model="formDate.status" placeholder="请选择">
              <el-option v-for="item in statusList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-form>
      </template>
    </HeaderTab>
    <el-card>
      <el-button round style="background-color:#55bb7c;color:#fff;" @click="addFn">新增仓库</el-button>
      <el-table border :data="list">
        <el-table-column label="序号" type="index" width="60" />
        <el-table-column label="仓库编码" width="150" align="center" prop="code" />
        <el-table-column label="仓库名称" width="150" align="center" prop="name" />
        <el-table-column label="仓库类型" width="120" align="center">
          <template #default="scope">
            {{ typeMap[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column label="省/市/区" width="170" align="center" prop="location" />
        <el-table-column label="详细地址" width="150" align="center" prop="address" />
        <el-table-column label="仓库状态" width="150" align="center">
          <template #default="scope">
            {{ statueMap[+scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column label="仓库面积m²" width="150" align="center" prop="surface" />
        <el-table-column label="库区数量" width="100" align="center" prop="includedNum" />
        <el-table-column label="负责人" width="150" align="center" prop="personName" />
        <el-table-column label="联系电话" width="150" align="center" prop="phone" />
        <el-table-column label="更新时间" width="160" align="center" prop="updateTime" />
        <el-table-column label="操作" fixed="right" width="150" align="center">
          <template #default="scope">
            <el-button type="text" size="small" @click="update(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="change(scope.row)">{{ statueMap[+scope.row.status === 1 ? 0 : 1] }}</el-button>
            <el-button type="text" size="small" @click="delFn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <pageFooter
          :params="params"
          :total="total"
          @CurrentChange="CurrentChangeFn"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { warehpuseList, aupdataCK, delCK } from '@/api'
import HeaderTab from '@/components/HeaderTab'
import pageFooter from '@/components/pageFooter'
export default {
  components: {
    HeaderTab,
    pageFooter
  },

  data() {
    return {
      params: {
        current: 1,
        size: 10
      },
      total: 0,
      list: [],
      formDate: {
        like_code: '',
        like_name: '',
        status: ''
      },
      statusList: [
        {
          id: 0,
          name: '停用'
        },
        {
          id: 1,
          name: '启用'
        },
        {
          id: 2,
          name: '全部'
        }
      ],
      statueMap: {
        0: '停用',
        1: '启用'
      },
      typeMap: {
        'ZZ': '中转仓',
        'JG': '加工仓',
        'CB': '储备仓',
        'LC': '冷藏仓'
      }
    }
  },
  created() {
    this.fetchList(this.params)
  },
  methods: {
    async fetchList(obj) {
      const { data: { data }} = await warehpuseList(obj)
      // console.log(res)
      this.list = data.records
      // console.log(data)
      // this.page = +data.pages
      this.total = +data.total
      // console.log(this.page, this.total)
    },
    CurrentChangeFn(val) {
      // console.log(`当前页: ${val}`)
      this.params.current = +val
      this.fetchList(this.params)
    },
    addFn() {
      this.$router.push('/details/null')
    },
    update(row) {
      // console.log(row)
      localStorage.setItem('CKID', JSON.stringify(row.id))
      this.$router.push({
        path: `/details/${row.id}`,
        params: row.id
      })
    },
    change(row) {
      // console.log(row)
      this.$confirm(`确认要${this.statueMap[+row.status === 1 ? 0 : 1]}吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        if (+row.status === 1) {
          row.status = 0
          // await aupdataCK()
        } else if (+row.status === 0) {
          row.status = 1
        }
        // 更新仓库
        await aupdataCK(row)
      }).catch((error) => {
        console.log(error)
      })
    },
    async delFn(row) {
      await delCK(row.id)
    }
  }
}

</script>
<style lang='scss' scoped>
.el-card {
  border-radius: 23px;
  margin: 0 16px 16px;
}
// .el-button {
//   background-color: #55bb7c;
//   color: #fff;
// }
.el-table {
  margin-top: 15px;
}
.block {
  margin-top: 10px;
  margin-left: 330px;
}
</style>
